/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"406B00AE-E961-42E0-A40F-63C82270F458"}
 */
var fconv = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"E2C68E0E-53FA-4F45-BFB9-09FE717C03DA",variableType:8}
 */
var fdolar = null;

/**
 * @type {String}
 *
 * @properties={typeid:35,uuid:"7C9AD405-4B2B-43DB-8E9F-B676ED9341DB"}
 */
var fmon = null;

/**
 * @type {Number}
 *
 * @properties={typeid:35,uuid:"9BB7DB12-0825-4D8D-9A8D-00A8E7C47745",variableType:4}
 */
var ftarea = null;

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"EDF97D4D-0560-46CC-A81A-B7A6F986A00E"}
 */
function onShow(firstShow, event) {
	if (moneda_carga == 1)
		fmon = "$"
	else
		fmon = "u$s"
	
	dolar = globals.gBuscarCotizacion(fecha)
	
	globals.gtotal_debe = 0
	globals.gtotal_haber = 0
	//forms.limbo_modif_det.controller.sort("columna asc")
	
	for (var i = 1 ; i <= limbo_cab_to_limbo.getSize() ; i++)
	{
		limbo_cab_to_limbo.setSelectedIndex(i)		
		limbo_cab_to_limbo.importe_carga_debe 	= limbo_cab_to_limbo.importe_debe
		limbo_cab_to_limbo.importe_carga_haber 	= limbo_cab_to_limbo.importe_haber
		globals.gtotal_debe 					+= limbo_cab_to_limbo.importe_debe
		globals.gtotal_haber 					+= limbo_cab_to_limbo.importe_haber	
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"681C15D0-2C05-4A10-86D3-64DC8480859A"}
 */
function onAction_alta_item(event) {
	limbo_cab_to_limbo.newRecord()
	forms.limbo_modif_det.elements.cuentas_id.requestFocus()
	forms.limbo_modif_det.leyenda = " " 
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"6D114A82-21E3-4A00-83FC-0CE0F7E6CD9A"}
 */
function onAction_cancelar(event) {
	var res = globals.DIALOGS.showQuestionDialog('','Esta seguro que desea Salir?','SI','NO')    	
	if (res == 'NO')
	{
		return false
	}
	else 
	{
		limbo_cab_to_limbo.deleteAllRecords()
		controller.deleteRecord()
		databaseManager.setAutoSave(true)
		var $win = application.getWindow('ASIENTO')
		$win.hide()
		$win.destroy()
	}
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"B05CDFA2-7AF1-4754-82BF-3BE07C849FF3"}
 */
function onAction_eliminar(event) 
{
	var res = globals.DIALOGS.showQuestionDialog('','Revierte el Asiento?','SI','NO')    	
	if (res == 'SI')
	{
		ftarea = 2
		Actualizar_comun(ftarea)
	}
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"7D8233F6-8DB2-457A-9B73-E0B13BD80068"}
 */
function onAction_confirmar(event) {
	var res = globals.DIALOGS.showQuestionDialog('','Copia el Asiento?','SI','NO')    	
	if (res == 'SI')
	{
		ftarea = 1
		Actualizar_comun(ftarea)
	}
}

/**
 * @properties={typeid:24,uuid:"FBD1C9ED-21BA-4911-8288-7C46D21DB299"}
 */
function fConversionDolares() {
	var vTotalD = 0
	var vTotalH = 0
	
	for (var i=1;i<=forms.limbo_modif_det.controller.getMaxRecordIndex();i++)
	{
		forms.limbo_modif_det.controller.setSelectedIndex(i)	
		
		if (moneda_carga == 1)//Pesos
		{
			var impdol = forms.limbo_modif_det.importe / dolar
			forms.limbo_modif_det.impodol = impdol.toFixed(2)*1
			
			if (forms.limbo_modif_det.columna == 'D')
				vTotalD += forms.limbo_modif_det.impodol
			else
				vTotalH += forms.limbo_modif_det.impodol 
		}
		else
		{
			var importe = forms.limbo_modif_det.impodol * dolar			
			forms.limbo_modif_det.importe = importe.toFixed(2)*1

			if (forms.limbo_modif_det.columna == 'D')
				vTotalD += forms.limbo_modif_det.importe
			else
				vTotalH += forms.limbo_modif_det.importe
		}
		
	}
	
	if (vTotalD != vTotalH)
	{
		var dif = vTotalD - vTotalH
		if (forms.limbo_modif_det.columna == 'H')
		{	
			if (moneda_carga == 1)//Pesos
				forms.limbo_modif_det.impodol += dif
			else
				forms.limbo_modif_det.importe += dif
		}
		else
		{
			if (moneda_carga == 1)//Pesos
				forms.limbo_modif_det.impodol -= dif
			else
				forms.limbo_modif_det.importe -= dif
		}
	}
}

/**
 * @properties={typeid:24,uuid:"14A9036E-3DC8-4996-8152-B96BA09F93C4"}
 * @AllowToRunInFind
 */
function Actualizar_comun(ptarea) {
	
	var error = 0
	
	var $cant = databaseManager.getFoundSetCount(limbo_cab_to_limbo)
	
	for (var i = 1 ; i <= $cant ; i++)
	{
		var $rcd = limbo_cab_to_limbo.getRecord(i)
		//forms.limbo_modif_det.controller.setSelectedIndex(i)
		
		if ($rcd.cuentas_id == null || $rcd.division == null || $rcd.centro == null)
		{
			error = 1
			globals.DIALOGS.showWarningDialog("Aviso","Existen campos sin completar","Ok")					
			break
		}
			
		if ($rcd.importe_carga_debe != null && $rcd.importe_carga_haber != null)
		{
			error = 1
			globals.DIALOGS.showWarningDialog("Aviso","Debe y Haber con importes","Ok")
			break
		}
		
		if ($rcd.importe_carga_debe == null || $rcd.importe_carga_debe == 0)
		{
			var imp = $rcd.importe_carga_haber.toFixed(2)*1
			if (ptarea == 2)
			{
				$rcd.columna = 'D'
			}
			else
			{
				$rcd.columna = 'H'
			}
		}	
		else
		{
			var imp = $rcd.importe_carga_debe.toFixed(2)*1
			if (ptarea == 2)
			{
				$rcd.columna = 'H'
			}
			else
			{
				$rcd.columna = 'D'
			}
		}
		if (moneda_carga == 1)
			$rcd.importe = imp
		else
			$rcd.impodol = imp
	}
		
	if (globals.gtotal_debe != globals.gtotal_haber)
	{
	    error = 1
		globals.DIALOGS.showWarningDialog("Aviso","El Total del Debe no coincide con el Total del Haber","Ok")
	}	
	if (error == 1)			
		globals.DIALOGS.showWarningDialog("Aviso","Asiento sin actualizar","Ok")			
	else
	{
		if (globals.gtotal_debe != total)
		{
			var res = globals.DIALOGS.showQuestionDialog('','Total diferente, continua?','SI','NO')			    
			if (res == 'NO')
			{
				return
			}
		}
		var vAño = fecha.getFullYear()
		var vMes = fecha.getMonth()+1
		/** @type {JSFoundSet<db:/contable/numcon>}*/ 
		var fs_numcon = databaseManager.getFoundSet('contable','numcon')
		
		fs_numcon.find()
		fs_numcon.anio 		= vAño
		fs_numcon.mes 		= vMes
		fs_numcon.empresa 	= globals.gUservID
		
		if (fs_numcon.search() == 0)
		{	
	        globals.DIALOGS.showWarningDialog("Aviso","Fecha inválida","Ok")
			return
		}
		else
		{
			if (fs_numcon.marca_cierre == 1)
			{
				globals.DIALOGS.showWarningDialog("Aviso","Mes Cerrado","Ok")
				return
			}
		}
		
		fConversionDolares()
		numero 		= fs_numcon.limbo
		provisorio 	= fs_numcon.limbo
		empresa  	= globals.gUservID
		hizo  		= globals.glegajo
		total 		= globals.gtotal_debe
	
		fs_numcon.limbo++
    	
		var estado = databaseManager.saveData()
		if (estado == false){
			globals.DIALOGS.showWarningDialog("Aviso","No se actualizaron los datos","Ok")
			databaseManager.revertEditedRecords()
			return
		}
		else
    	{
			if (ptarea == 1)
			{
				globals.DIALOGS.showWarningDialog("Aviso","Asiento copiado con exito","Ok")				
			}
			else
			{
				globals.DIALOGS.showWarningDialog("Aviso","Asiento revertido con exito","Ok")
			}
    	}
    	databaseManager.setAutoSave(true)
        var $win = application.getWindow('ASIENTO')
		$win.hide()
		$win.destroy()
	}
}
